package com.cyandpos.pad.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;

import com.changyi.device.APApplication;
import com.cyandpos.pad.R;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.posdata.ClassNewspaperAndDaily;
import com.cyandpos.pad.report.ReportPrint;
import com.cyandpos.pad.report.adapter.ViewPagerAdapter;
import com.cyandpos.pad.report.reportPresenter.ReportPresent;
import com.cyandpos.pad.report.reportView.CashReportView;
import com.cyandpos.pad.report.reportView.IReportView;
import com.cyandpos.pad.report.reportView.TodayReportView;
import com.cyandpos.pad.utils.MoneyUtils;
import com.cyandpos.pad.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class ReportActivity extends AppCompatActivity implements IReportView, View.OnClickListener {

    /**
     * 代表本班班报已经进行过网络请求
     */
    private final int CASHREPORT_HAD_DO_HTTP = 0b10;
    /**
     * 代表收银日报已经进行过网络请求
     */
    private final int TODAYREPORT_HAD_DO_HTTP = 0b01;
    private final int BOTH_HAD_DO_HTTP = 0b11;
    private int select_num = 0;
    private ReportPresent reportPresent;
    /**
     * 视图集合，用于显示交易详情，包括CashReportView和TodayReportView。
     */
    private List<View> viewList;
    private ViewPager viewPager;
    /**
     * 标题按钮
     */
    private RadioButton rBtn_report1, rBtn_report2;


    /**
     * 标题栏的回退按钮
     */
    private Toolbar toolbar;
    /**
     * 打印按钮
     */
    private Button btn_report_print;
    private TextView tv_dialog_title;

    private ProgressDialog dialog;
    private ClassNewspaperAndDaily daily;

    /**
     * 打印按钮
     */
    private Button btn_report_btn_backgroud_print;
    CashReportView cashReportView;
    TodayReportView todayReportView;

    int can_bb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
        APApplication.getInstance().addActivity(this);
        Intent intent = getIntent();
        can_bb = intent.getIntExtra("can_bb", 0);
        initView();
        HttpSelector();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 网络请求选择发生器
     */
    private void HttpSelector() {
        if (rBtn_report1.isChecked()) {
            int skyid = Integer.parseInt(ErpCommonData.loadConfig.getCashierId());
                reportPresent.reportData(skyid);//收款员或营业员id, -1 表示无，查收银日报，正常查本班日报
        } else if (rBtn_report2.isChecked()) {

                reportPresent.reportData(-1);//收款员或营业员id, -1 表示无，查收银日报
        } else {
            ToastUtils.show("页面状态错误，请刷新");
        }
    }

    private void initView() {
        dialog = new ProgressDialog(ReportActivity.this);
        dialog.setCancelable(false);
        reportPresent = new ReportPresent(this);
        daily = new ClassNewspaperAndDaily();
        initPagerTitle();
        initViewPager();
        initActivityView();
    }
    /**
     * activity 中其他的基本控件初始化
     */
    private void initActivityView() {
        btn_report_btn_backgroud_print = (Button) findViewById(R.id.btn_report_btn_backgroud_print);
        tv_dialog_title = (TextView) findViewById(R.id.tv_dialog_title_name);
        btn_report_btn_backgroud_print.setOnClickListener(this);
        tv_dialog_title.setOnClickListener(this);
    }




    /**
     * 初始化PagerTitle
     */
    private void initPagerTitle() {
        rBtn_report1 = (RadioButton) findViewById(R.id.rbtn_report_1);
        rBtn_report2 = (RadioButton) findViewById(R.id.rbtn_report_2);
        rBtn_report1.setOnClickListener(new MyOnClickListener(0));
        rBtn_report2.setOnClickListener(new MyOnClickListener(1));
    }


    public void doPrint(ClassNewspaperAndDaily daily){
        ReportPrint reportPrint;
        if (rBtn_report1.isChecked()) {
            reportPrint = new ReportPrint(this, daily, 1);
            reportPrint.doPrint();
        } else if (rBtn_report2.isChecked()) {
            reportPrint = new ReportPrint(this, daily, 2);
            reportPrint.doPrint();
        } else {
            ToastUtils.show("页面状态错误，请刷新");
        }
    }
    /**
     * 打印按钮的监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_report_btn_backgroud_print: {
                doPrint(daily);
                finish();
            }
            break;
            case R.id.tv_dialog_title_name:
                 finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void showProgress() {
        dialog.show();
        dialog.setMessage("loading...");
    }

    @Override
    public void hideProgress() {
        dialog.dismiss();
    }

    @Override
    public void onQueryDataSuccess(ClassNewspaperAndDaily daily) {
        this.daily = daily;
        if (rBtn_report1.isChecked()) {
            cashReportView.dateChanged(daily.getReportPayList());
            cashReportView.setShopAssistantName(ErpCommonData.loadConfig.getCashierName());
            cashReportView.setTotalTradeMoney(MoneyUtils.getMoneyInt(daily.getTotal_sale()));
            cashReportView.setTotalBackNo(daily.getCount_back());
            cashReportView.setTotalBackMoney(MoneyUtils.getMoneyInt(daily.getTotal_back()));
            cashReportView.setTotalTradeNo(daily.getCount_sale());
            cashReportView.setDepartmentCode(ErpCommonData.loadConfig.getCashierDeptCode());
        } else if (rBtn_report2.isChecked()) {
            todayReportView.dateChanged(daily.getReportPayList());
            todayReportView.setTotalTradeMoney(MoneyUtils.getMoneyInt(daily.getTotal_sale()));
            todayReportView.setTotalBackNo(daily.getCount_back());
            todayReportView.setTotalBackMoney(MoneyUtils.getMoneyInt(daily.getTotal_back()));
            todayReportView.setTotalTradeNo(daily.getCount_sale());
        } else {
            ToastUtils.show("页面状态错误，请刷新");
        }

    }

    @Override
    public void showLoadFailMsg(String errorMsg) {
        ToastUtils.show(errorMsg);
    }


    /**
     * 头标点击监听
     */
    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            viewPager.setCurrentItem(index);
        }
    }


    /**
     * 初始化viewpager
     */
    private void initViewPager() {
        cashReportView = new CashReportView(ReportActivity.this);
        todayReportView = new TodayReportView(ReportActivity.this);
        viewPager = (ViewPager) findViewById(R.id.vp_report);

        viewList = new ArrayList<>();// 将要分页显示的View装入数组中
        if (can_bb == 2) {
            rBtn_report2.setVisibility(View.GONE);
            rBtn_report1.setVisibility(View.VISIBLE);
            rBtn_report1.setChecked(true);
            rBtn_report2.setChecked(false);
            viewList.add(cashReportView);
        } else if (can_bb == 3) {
            rBtn_report1.setVisibility(View.GONE);
            rBtn_report2.setVisibility(View.VISIBLE);
            rBtn_report2.setChecked(true);
            rBtn_report1.setChecked(false);
            viewList.add(todayReportView);
        } else {
            viewList.add(cashReportView);
            viewList.add(todayReportView);
        }
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(viewList);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                HttpSelector();
                switch (position) {
                    case 0:
                        rBtn_report1.setChecked(true);
                        break;
                    case 1:
                        rBtn_report2.setChecked(true);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        viewPager.setAdapter(viewPagerAdapter);
    }
}


